package tech.yangzhijie.basic;



import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class TestDemo {

    @Test
    public void method01(){
        Person p = new Police();
        p.method01();//
        int i = p.add01(3, 3);
        System.out.println(i);
    }

    @Test
    public void method02(){
        //函数式接口
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<10;i++){
                    System.out.println("没用Lambda表达式");
                }
            }
        }).start();
        new Thread(()->{
            for (int i=0;i<10;i++){
                System.out.println("Lambda表达式");
            }
        }).start();
    }

    @Test
    public void method03(){
        //System.out.println(Instant.MIN + "  " + Instant.MAX);
        /*Instant start = Instant.now();
        new Thread(()->{
            int c = 0;
            for(int i=0;i<1000;i++){
                c+=i;
            }
            System.out.println(c);
        }).start();
        Instant end = Instant.now();
        Duration duration = Duration.between(start,end);
        System.out.println(duration.toMillis());*/

        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(1996,9,21);
        //localDate = localDate.plusDays(2);//当前时间增加两天
        //localDate = localDate.plusWeeks(1);//当前时间添加一周 7天
        //localDate = localDate.plusMonths(1);//增加一个月
        //localDate = localDate.plusYears(-1);//减少一年
        //localDate = localDate.minusDays(1);//减少一天
        Duration d = Duration.ofDays(1);
        System.out.println(d.toMillis());
        //localDate = localDate.minus(d);
        System.out.println(localDate);
        System.out.println(localDate1);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

        LocalDateTime time = LocalDateTime.now();
        System.out.println("-->"+time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        LocalTime time1 = LocalTime.now();
        System.out.println(time1);

        System.out.println(localDate.isLeapYear());
    }

    @Test
    public void method04(){
        String str = String.join(".","www","sea","com");
        System.out.println(str);
        System.out.println("----------------");
        System.out.print(Byte.BYTES);//1
        System.out.print(Short.BYTES);//2
        System.out.print(Integer.BYTES);//4
        System.out.print(Long.BYTES);//8
        System.out.print(Float.BYTES);//4
        System.out.print(Double.BYTES);//8
        System.out.println(Character.BYTES);//2
        System.out.println("----------------");
        System.out.println(Byte.hashCode((byte)1));
    }

    @Test
    public void method05(){
        Object[] arr = {1,2,3};
        List<Object> arr2 = new ArrayList<>(2);
        for(int i=0;i<arr.length;i++){
            if(Integer.valueOf(arr[i].toString())>1){
                arr2.add(arr[i]);
            }
        }
        for(Object o:arr){
            if(Integer.valueOf(o.toString())>1){
                arr2.add(o);
            }
        }
        System.out.println(arr2);
        /** 使用流过滤 */
        List<Object> collect = Stream.of(arr).filter(num -> Integer.valueOf(num.toString()) > 1).collect(toList());
        System.out.println(collect);
        IntStream range = IntStream.range(0, 100);
        //System.out.println(range.collect());
    }


    @Test
    public void method06(){
        List<Integer> list = new ArrayList<>();
        for (int j = 0; j < 100000; j++) {
            list.add(j+1);
        }
        long time = System.currentTimeMillis();
        int i = -1;
        System.out.println(putongSearch(i, list));
        System.out.println(System.currentTimeMillis() - time);
    }

    @Test
    public void method07(){
        List<Integer> list = new ArrayList<>();
        for (int j = 0; j < 100000; j++) {
            list.add(j+1);
        }
        long time = System.currentTimeMillis();
        int i = -1;
        System.out.println(binarySearch(i, list));
        System.out.println(System.currentTimeMillis() - time);
    }

    public int putongSearch(int i,List<Integer> list){
        for (int j = 0; j < list.size(); j++) {
            Integer integer =  list.get(j);
            if(integer == i){
                return j;
            }
        }
        return -1;
    }

    public int binarySearch(int i,List<Integer> list){
        int start = 0;
        int end = list.size()-1;//下标从0到元素个数减一
        while (start <= end){
            int center = start + (end - start)/2;//中间数下标
            if(list.get(center) < i){
                start = center + 1;
            }else if(list.get(center) > i){
                end = center - 1;
            }else {
                return center;
            }
        }
        return -1;
    }
}
